﻿using System;
using System.Text;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MIMSDomain.Abstract;
using MIMSDomain.Entities;
using Moq;
using System.Linq;
using MIMSWeb.Controllers;
using System.Web.Mvc;
using MIMSCore.Abstract;
using MIMSCore.Logging;

namespace MIMSUnitTests.Web.Controllers
{
    /// <summary>
    /// Summary description for MedicalPersonnelsControllersTests
    /// </summary>
    [TestClass]
    public class MedicalPersonnelsControllersTests : IDisposable
    {
        public IRepository<MedicalPersonnel> MockMedicalPersonnelRepository;
        public ILogger<SynkronLogger> logger;
        public MedicalPersonnelsController controller;

        [TestInitialize]
        public void Initialize(){
            //Arrange
            IList<MedicalPersonnel> medicalPersonnels = new List<MedicalPersonnel>
            {
                new MedicalPersonnel{Id = 1, FirstName="James", MiddleName="Robertson", LastName="Akinlade", MedicalPersonnelType = MedicalPersonnelType.Doctor, Sex = "Male"},
                new MedicalPersonnel{Id = 2, FirstName="Funsho", MiddleName="Alaranmbe", LastName="Babayemi", MedicalPersonnelType = MedicalPersonnelType.Nurse, Sex = "Female"},
                new MedicalPersonnel{Id = 3, FirstName="Patrick", MiddleName="Shodeinde", LastName="Gbeminiyi", MedicalPersonnelType = MedicalPersonnelType.Pharmacist, Sex = "Female"},
                new MedicalPersonnel{Id = 4, FirstName="Sola", MiddleName="Afolabi", LastName="Olorunsogo", MedicalPersonnelType = MedicalPersonnelType.Nurse, Sex = "Female"},
                new MedicalPersonnel{Id = 5, FirstName="Okon", MiddleName="Wale", LastName="Babajide", MedicalPersonnelType = MedicalPersonnelType.Doctor, Sex = "Male"},
                new MedicalPersonnel{Id = 6, FirstName="Amarachi", MiddleName="Ohiaeri", LastName="Akinlade", MedicalPersonnelType = MedicalPersonnelType.Doctor, Sex = "Male"}
            };

            var target = new Mock<IRepository<MedicalPersonnel>>();
            target.Setup(p => p.All).Returns(medicalPersonnels.AsQueryable());
            MockMedicalPersonnelRepository = target.Object;

            logger = new Mock<ILogger<SynkronLogger>>().Object;
            //Act
            controller = new MedicalPersonnelsController(MockMedicalPersonnelRepository, logger);
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void Index()
        {
            //Act
            var actual = controller.Index() as ViewResult;

            //Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(6, ((IQueryable<MedicalPersonnel>)(actual.Model)).Count());
            Assert.IsInstanceOfType(actual.Model, typeof(IQueryable<MedicalPersonnel>));
            Assert.AreEqual("Index", actual.ViewName);
        }
        [TestMethod]
        public void AddMedicalPersonnel()
        {
            //Act
            var actual = controller.AddMedicalPersonnel();
            Assert.IsNotNull(actual);
            Assert.AreEqual("", actual.ViewName);

            actual = controller.AddMedicalPersonnel(new MedicalPersonnel { FirstName="Korede", MiddleName="Adewunmi", LastName="Olaleye", 
                MedicalPersonnelType = MedicalPersonnelType.Doctor, Sex = "Male", Code ="666" });
            Assert.IsNotNull(actual);
            Assert.IsInstanceOfType(actual.Model, typeof(MedicalPersonnel));
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                controller.Dispose();
            }
        }
    }
}
